import { ref, onMounted, onUnmounted } from 'vue';

export function useAutoSave<T>(
  data: T,
  saveFn: (_data: T) => void,
  interval = 30000
) {
  const timer = ref<number | null>(null);

  onMounted(() => {
    timer.value = window.setInterval(() => {
      saveFn(data);
    }, interval);
  });

  onUnmounted(() => {
    if (timer.value) {
      clearInterval(timer.value);
    }
  });

  return {};
}
